package session.TX;

import entity.PurchasedItem_Regular;
import Helper.TTAHelper.Helper_PIPSales;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Lu Xiaodi
 */
@Stateless
public class PIPSB implements PIPBeanRemote {

    @PersistenceContext
    EntityManager em;

    public List<Calendar> getAvailableMonths(){
        Calendar cEnd = Calendar.getInstance();
        cEnd.setTime(new Date());
        Calendar cBegin = Calendar.getInstance();
        cBegin.set(cEnd.get(Calendar.YEAR)-2, cEnd.get(Calendar.MONTH), cEnd.get(Calendar.DATE),0,0,0);
        List<Calendar> list = new ArrayList<Calendar>();
        while (cBegin.before(cEnd)){
            Calendar c = (Calendar)cBegin.clone();
            
            list.add(c);
            cBegin.add(Calendar.MONTH, 1);
            System.out.println(cBegin.get(Calendar.YEAR)+ ",  " + cBegin.get(Calendar.MONTH));
        }
        return list;
    }
    
    public List<PurchasedItem_Regular> getSalesReportItems(Long outletId, Calendar start, Calendar end) {
        Query query = em.createQuery("Select p FROM PurchasedItem_Regular p WHERE p.outlet_id = :oId AND "
                + "p.time_stamp >= :start AND p.time_stamp <= :end GROUP BY p.time_stamp");
        query.setParameter("oId", outletId);
        query.setParameter("start", start);
        query.setParameter("end", end);
        return query.getResultList();
    }

    public List<Helper_PIPSales> getSalesData(Long outletId, Calendar start, Calendar end) {
        List<PurchasedItem_Regular> items = this.getSalesReportItems(outletId, start, end);
        List<Helper_PIPSales> returnList = new ArrayList<Helper_PIPSales>();

        //if null, just create a list of dummy helpers.
        if (items == null || items.isEmpty()) {
            return null;
        }

        Calendar cal = items.get(0).getTime_stamp();
        double total = 0;

        for (int i = 0; i < items.size(); i++) {
            PurchasedItem_Regular item = items.get(i);
            Calendar currCal = item.getTime_stamp();
            currCal.get(Calendar.MONTH);
            currCal.get(Calendar.YEAR);
            if (currCal.get(Calendar.MONTH) == cal.get(Calendar.MONTH)
                    && currCal.get(Calendar.YEAR) == cal.get(Calendar.YEAR)) {
                total += item.getSubtotal();
            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
                Helper_PIPSales aHelper = new Helper_PIPSales(calendar, total);
                returnList.add(aHelper);
                cal = currCal;
                total = item.getSubtotal();
            }
            if (i == items.size() - 1) {
                Helper_PIPSales aHelper = new Helper_PIPSales(currCal, total);
                returnList.add(aHelper);
            }
        }

        System.out.println(returnList.size());
        return returnList;
    }

    private boolean isOneMonthLater(Calendar sc, Calendar ec) {
        int sm = sc.get(sc.MONTH);
        int sy = sc.get(sc.YEAR);
        int em = sc.get(ec.MONTH);
        int ey = sc.get(ec.YEAR);
        boolean returnBool = false;
        if (sm != 12) {
            if (sy == ey && sm + 1 == em) {
                returnBool = true;
            }
        } else {
            if (em == 1 && sy + 1 == ey) {
                returnBool = true;
            }
        }
        return returnBool;
    }

    private boolean isSameMon(Calendar c1, Calendar c2) {
        if (c1.get(c1.YEAR) == c2.get(c2.YEAR) && c1.get(c1.MONTH) == c2.get(c2.MONTH)) {
            return true;
        }
        return false;
    }

}
